package com.crazymaker.springcloud.base.config;

import com.crazymaker.springcloud.base.dao.SysUserDao;
import com.crazymaker.springcloud.base.dao.UserDao;
import com.crazymaker.springcloud.base.filter.CustomedSessionRepositoryFilter;
import com.crazymaker.springcloud.base.filter.RedisSessionFilter;
import com.crazymaker.springcloud.base.filter.SessionPrefixFilter;
import com.crazymaker.springcloud.base.filter.SessionSeedFilter;
import com.crazymaker.springcloud.base.service.impl.UserLoadService;
import com.crazymaker.springcloud.standard.redis.RedisRepository;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.session.data.redis.RedisOperationsSessionRepository;

@Configuration
public class RedisFilterConfig
{

    public static final int START = 1000;

    @Bean
    public FilterRegistrationBean buildSessionPrefixFilter()
    {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setOrder(1);
        filterRegistrationBean.setFilter(new SessionPrefixFilter());
        filterRegistrationBean.setName("SessionPrefixFilter" );
        filterRegistrationBean.addUrlPatterns("/*" );
        return filterRegistrationBean;
    }

    @Bean
    public FilterRegistrationBean buildSessionHeadNameFilter(
            RedisRepository redisRepository,
            RedisOperationsSessionRepository sessionRepository)
    {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setOrder(1 + START);
        filterRegistrationBean.setFilter(new SessionSeedFilter(redisRepository, sessionRepository));
        filterRegistrationBean.setName("SessionSeedFilter" );
        filterRegistrationBean.addUrlPatterns("/*" );
        return filterRegistrationBean;
    }

    @ConditionalOnBean(UserDao.class)
    @DependsOn({"userDao", "sysUserDao"})
    @Bean
    UserLoadService userLoadService(UserDao userDao,
                                    SysUserDao sysUserDao)
    {
        return new UserLoadService(userDao, sysUserDao);
    }

    @DependsOn({"httpSessionIdResolver", "sessionRepository"})
    @Bean
    public FilterRegistrationBean buildSpringSessionRepositoryFilter(CustomedSessionIdResolver httpSessionIdResolver,
                                                                     RedisOperationsSessionRepository sessionRepository)
    {


        CustomedSessionRepositoryFilter sessionRepositoryFilter = new CustomedSessionRepositoryFilter<>(
                sessionRepository);
//        sessionRepositoryFilter.setServletContext(this.servletContext);
        sessionRepositoryFilter.setHttpSessionIdResolver(httpSessionIdResolver);


        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setOrder(2 + START);
        filterRegistrationBean.setFilter(sessionRepositoryFilter);
        filterRegistrationBean.setName("springSessionRepositoryFilter" );
        filterRegistrationBean.addUrlPatterns("/*" );

        return filterRegistrationBean;
    }

    @ConditionalOnBean(UserLoadService.class)
    @DependsOn({"userLoadService", "redisRepository"})
    @Bean
    public FilterRegistrationBean buildRedisSessionFilter(
            UserLoadService userLoadService,
            RedisRepository redisRepository)
    {
        FilterRegistrationBean filterRegistrationBean =
                new FilterRegistrationBean();
        filterRegistrationBean.setOrder(3 + START);
        RedisSessionFilter rf =
                new RedisSessionFilter(userLoadService, redisRepository);
        filterRegistrationBean.setFilter(rf);
        filterRegistrationBean.setName("redisSessionFilter" );
        filterRegistrationBean.addUrlPatterns("/*" );
        return filterRegistrationBean;
    }


}